<template>
  <div class="app-container">
    <div class="custom_card_0" v-show="showSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="68px"
        size="small"
      >
        <el-form-item label1 label="车牌筛选" prop="carNo">
          <el-input
            v-model="queryParams.carNo"
            placeholder="车牌筛选"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label1 label="原因筛选" prop="manageReason">
          <el-select
            v-model="queryParams.manageReason"
            placeholder="原因筛选"
            clearable
          >
            <el-option
              v-for="dict in dict.type.reason_for_disposal"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="custom_card">
      <el-row :gutter="10" class="mb10">
        <el-col :span="1.5">
          <el-button
            class="first-plain-btn" size="medium"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['management:management:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            class="sec-plain-btn" size="medium"
            icon="el-icon-download"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['management:management:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
      <el-table
        v-loading="loading"
        :data="managementList"
        @selection-change="handleSelectionChange"
        border
      >
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="100"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleDetail(scope.row)"
              >查看
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="品牌/车系/车型"
          header-align="center"
          prop="carInfo"
          min-width="240"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="车牌号"
          align="center"
          prop="carNo"
          min-width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="车辆来源"
          align="center"
          prop="source"
          min-width="100"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.vehicle_source"
              :value="scope.row.source"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="归属门店"
          align="center"
          prop="belongingStoreName"
          min-width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="停靠门店"
          align="center"
          prop="locatedStoreName"
          min-width="100"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="处置时间"
          align="center"
          prop="manageTime"
          min-width="160"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.manageTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="处置类型"
          align="center"
          prop="manageReason"
          min-width="100"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.reason_for_disposal"
              :value="scope.row.manageReason"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="残值"
          align="center"
          prop="manageMoney"
          min-width="100"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            ￥{{ scope.row.manageMoney || '--' }}
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <managementAdd
      v-if="openAdd"
      :open="openAdd"
      @close="
        openAdd = false
        getList()
      "
    ></managementAdd>
  </div>
</template>

<script>
import {
  listManagement,
  getManagement,
  delManagement,
  addManagement,
  updateManagement,
  exportManagement,
} from '@/api/car/management'
import managementAdd from '@/views/car/management/add.vue'
export default {
  name: 'Management',
  dicts: ['reason_for_disposal', 'vehicle_source'],
  components: {
    managementAdd,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆处置表格数据
      managementList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      openAdd: false,
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNo: null,
        manageReason: null,
        manageTime: null,
        manageMoney: null,
        deptId: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询车辆处置列表 */
    getList() {
      this.loading = true
      listManagement(this.queryParams).then((response) => {
        this.managementList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        carId: null,
        manageReason: '0',
        manageTime: null,
        manageMoney: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: null,
        tenantId: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleDetail(row) {
      console.log(row.carId)
      this.$router.push({ name: 'management_detail', query: { id: row.carId } })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.openAdd = true
      this.title = '添加车辆处置'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getManagement(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改车辆处置'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateManagement(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addManagement(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除车辆处置编号为"' + ids + '"的数据项？')
        .then(function () {
          return delManagement(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal
        .confirm('是否确认导出所有车辆处置数据项？')
        .then(() => {
          this.exportLoading = true
          return exportManagement(queryParams)
        })
        .then((response) => {
          this.$download.name(response.msg)
          this.exportLoading = false
        })
        .catch(() => {})
    },
  },
}
</script>

